package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.wreport.WReportDetailEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22 0022.
 */
@Repository
public class WReportDetailDao extends BaseJdbcDao {

    public Integer add(WReportDetailEntity model) {
        model.setId(super.generateKey());
        return super.insert("T_WRT_DETAIL", model.getJSONObject());
    }

    public  Integer update(WReportDetailEntity model){
        return  super.update("T_WRT_DETAIL", model.getJSONObject(),model.getJSONObjectId());
    }

    //物理删除
    public Integer remove(String id){
        String sql = "delete from T_WRT_DETAIL where ID = ?";
        String[] params = {id};
        return  super.delete(sql,params);
    }

    public Integer del(WReportDetailEntity model){
        return 0 ;
    }

    public WReportDetailEntity getById(String id){
        String condition = " AND ID = ? ";
        String[] params = {id};
        List<WReportDetailEntity> list = this.getByCondition(condition,params);
        if ( list != null && list.size() > 0 ){
            return list.get(0);
        } else {
            return null;
        }
    }

    public List<WReportDetailEntity> getByCondition(String condition,Object[] params){
        return this.getByPage(condition,params,1,Integer.MAX_VALUE);
    }

    public List<WReportDetailEntity> getByPage(String condition,Object[] params, int requestPage, int pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id, \n");
        sql.append(" CONTENT AS content, \n");
        sql.append(" TYPE AS type, \n");
        sql.append(" W_ID AS wId \n");
        sql.append(" FROM T_WRT_DETAIL \n");
        sql.append(" WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(" ORDER BY SORT_NO");
        super.appendPageSql(sql,requestPage,pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);

        List<WReportDetailEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportDetailEntity model = JSON.toJavaObject(jsonList.get(i),WReportDetailEntity.class);
            list.add(model);
        }
        return list;
    }


    public int getCount(String condition,Object[] params){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" COUNT(1) \n");
        sql.append(" FROM T_WRT_DETAIL \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(condition);
        String count = super.queryForString(sql.toString(),params);
        return Integer.parseInt(count);
    }

}

